{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting data/train-images-idx3-ubyte.gz\n",
      "Extracting data/train-labels-idx1-ubyte.gz\n",
      "Extracting data/t10k-images-idx3-ubyte.gz\n",
      "Extracting data/t10k-labels-idx1-ubyte.gz\n",
      "MNIST ready\n"
     ]
    }
   ],
   "source": [
    "mnist = input_data.read_data_sets('data/', one_hot=True)\n",
    "trainimg   = mnist.train.images\n",
    "trainlabel = mnist.train.labels\n",
    "testimg    = mnist.test.images\n",
    "testlabel  = mnist.test.labels\n",
    "print (\"MNIST ready\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "n_input  = 784\n",
    "n_output = 10\n",
    "weights  = {\n",
    "        'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.1)),\n",
    "        'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.1)),\n",
    "        'wd1': tf.Variable(tf.random_normal([7*7*128, 1024], stddev=0.1)),\n",
    "        'wd2': tf.Variable(tf.random_normal([1024, n_output], stddev=0.1))\n",
    "    }\n",
    "biases   = {\n",
    "        'bc1': tf.Variable(tf.random_normal([64], stddev=0.1)),\n",
    "        'bc2': tf.Variable(tf.random_normal([128], stddev=0.1)),\n",
    "        'bd1': tf.Variable(tf.random_normal([1024], stddev=0.1)),\n",
    "        'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1))\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CNN READY\n"
     ]
    }
   ],
   "source": [
    "def conv_basic(_input, _w, _b, _keepratio):\n",
    "        # INPUT\n",
    "        _input_r = tf.reshape(_input, shape=[-1, 28, 28, 1])\n",
    "        # CONV LAYER 1\n",
    "        _conv1 = tf.nn.conv2d(_input_r, _w['wc1'], strides=[1, 1, 1, 1], padding='SAME')\n",
    "        #_mean, _var = tf.nn.moments(_conv1, [0, 1, 2])\n",
    "        #_conv1 = tf.nn.batch_normalization(_conv1, _mean, _var, 0, 1, 0.0001)\n",
    "        _conv1 = tf.nn.relu(tf.nn.bias_add(_conv1, _b['bc1']))\n",
    "        _pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')\n",
    "        _pool_dr1 = tf.nn.dropout(_pool1, _keepratio)\n",
    "        # CONV LAYER 2\n",
    "        _conv2 = tf.nn.conv2d(_pool_dr1, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME')\n",
    "        #_mean, _var = tf.nn.moments(_conv2, [0, 1, 2])\n",
    "        #_conv2 = tf.nn.batch_normalization(_conv2, _mean, _var, 0, 1, 0.0001)\n",
    "        _conv2 = tf.nn.relu(tf.nn.bias_add(_conv2, _b['bc2']))\n",
    "        _pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')\n",
    "        _pool_dr2 = tf.nn.dropout(_pool2, _keepratio)\n",
    "        # VECTORIZE\n",
    "        _dense1 = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]])\n",
    "        # FULLY CONNECTED LAYER 1\n",
    "        _fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1']))\n",
    "        _fc_dr1 = tf.nn.dropout(_fc1, _keepratio)\n",
    "        # FULLY CONNECTED LAYER 2\n",
    "        _out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2'])\n",
    "        # RETURN\n",
    "        out = { 'input_r': _input_r, 'conv1': _conv1, 'pool1': _pool1, 'pool1_dr1': _pool_dr1,\n",
    "            'conv2': _conv2, 'pool2': _pool2, 'pool_dr2': _pool_dr2, 'dense1': _dense1,\n",
    "            'fc1': _fc1, 'fc_dr1': _fc_dr1, 'out': _out\n",
    "        }\n",
    "        return out\n",
    "print (\"CNN READY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'Variable_8:0' shape=(3, 3, 1, 64) dtype=float32_ref>\n"
     ]
    }
   ],
   "source": [
    "a = tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.1))\n",
    "print (a)\n",
    "a = tf.Print(a, [a], \"a: \")\n",
    "init = tf.global_variables_initializer()\n",
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "#sess.run(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function max_pool in module tensorflow.python.ops.nn_ops:\n",
      "\n",
      "max_pool(value, ksize, strides, padding, data_format='NHWC', name=None)\n",
      "    Performs the max pooling on the input.\n",
      "    \n",
      "    Args:\n",
      "      value: A 4-D `Tensor` of the format specified by `data_format`.\n",
      "      ksize: A 1-D int Tensor of 4 elements.  The size of the window for\n",
      "        each dimension of the input tensor.\n",
      "      strides: A 1-D int Tensor of 4 elements.  The stride of the sliding\n",
      "        window for each dimension of the input tensor.\n",
      "      padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm.\n",
      "        See the @{tf.nn.convolution$comment here}\n",
      "      data_format: A string. 'NHWC', 'NCHW' and 'NCHW_VECT_C' are supported.\n",
      "      name: Optional name for the operation.\n",
      "    \n",
      "    Returns:\n",
      "      A `Tensor` of format specified by `data_format`.\n",
      "      The max pooled output tensor.\n",
      "\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "#print (help(tf.nn.conv2d))\n",
    "print (help(tf.nn.max_pool))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GRAPH READY\n"
     ]
    }
   ],
   "source": [
    "x = tf.placeholder(tf.float32, [None, n_input])\n",
    "y = tf.placeholder(tf.float32, [None, n_output])\n",
    "keepratio = tf.placeholder(tf.float32)\n",
    "\n",
    "# FUNCTIONS\n",
    "\n",
    "_pred = conv_basic(x, weights, biases, keepratio)['out']\n",
    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=_pred, labels=y))\n",
    "optm = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)\n",
    "_corr = tf.equal(tf.argmax(_pred,1), tf.argmax(y,1)) \n",
    "accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) \n",
    "init = tf.global_variables_initializer()\n",
    "    \n",
    "# SAVER\n",
    "print (\"GRAPH READY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 000/015 cost: 30.928401661\n",
      " Training accuracy: 0.500\n",
      "Epoch: 001/015 cost: 12.954609606\n",
      " Training accuracy: 0.700\n",
      "Epoch: 002/015 cost: 10.392489696\n",
      " Training accuracy: 0.700\n",
      "Epoch: 003/015 cost: 7.254891634\n",
      " Training accuracy: 0.800\n",
      "Epoch: 004/015 cost: 4.977767670\n",
      " Training accuracy: 0.900\n",
      "Epoch: 005/015 cost: 5.414173813\n",
      " Training accuracy: 0.600\n",
      "Epoch: 006/015 cost: 3.057567777\n",
      " Training accuracy: 0.700\n",
      "Epoch: 007/015 cost: 4.929724103\n",
      " Training accuracy: 0.600\n",
      "Epoch: 008/015 cost: 3.192437538\n",
      " Training accuracy: 0.600\n",
      "Epoch: 009/015 cost: 3.224479928\n",
      " Training accuracy: 0.800\n",
      "Epoch: 010/015 cost: 2.720530389\n",
      " Training accuracy: 0.400\n",
      "Epoch: 011/015 cost: 3.000342276\n",
      " Training accuracy: 0.800\n",
      "Epoch: 012/015 cost: 0.639763238\n",
      " Training accuracy: 1.000\n",
      "Epoch: 013/015 cost: 1.897303332\n",
      " Training accuracy: 0.900\n",
      "Epoch: 014/015 cost: 2.295500937\n",
      " Training accuracy: 0.800\n",
      "OPTIMIZATION FINISHED\n"
     ]
    }
   ],
   "source": [
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "\n",
    "training_epochs = 15\n",
    "batch_size      = 16\n",
    "display_step    = 1\n",
    "for epoch in range(training_epochs):\n",
    "    avg_cost = 0.\n",
    "    #total_batch = int(mnist.train.num_examples/batch_size)\n",
    "    total_batch = 10\n",
    "    # Loop over all batches\n",
    "    for i in range(total_batch):\n",
    "        batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n",
    "        # Fit training using batch data\n",
    "        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys, keepratio:0.7})\n",
    "        # Compute average loss\n",
    "        avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})/total_batch\n",
    "\n",
    "    # Display logs per epoch step\n",
    "    if epoch % display_step == 0: \n",
    "        print (\"Epoch: %03d/%03d cost: %.9f\" % (epoch, training_epochs, avg_cost))\n",
    "        train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})\n",
    "        print (\" Training accuracy: %.3f\" % (train_acc))\n",
    "        #test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel, keepratio:1.})\n",
    "        #print (\" Test accuracy: %.3f\" % (test_acc))\n",
    "\n",
    "print (\"OPTIMIZATION FINISHED\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
